package ATM_mysql;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存款界面
 */
public class SavePanel extends JPanel implements ActionListener {
    private ATM frame;
    private JLabel moneyLabel = new JLabel("请输入存入金额");
    private JTextField moneyTextField = new JTextField();
    private JButton sureBtn = new JButton(Util.getIcon("/images/buttonSure.jpg"));
    private JButton backBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));
    private Font font = new Font("宋体", Font.BOLD, 25);

    public SavePanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);//设置成透明

        moneyLabel.setBounds(300, 200, 200, 50);
        moneyLabel.setFont(font);
        this.add(moneyLabel);

        moneyTextField.setBounds(350, 250, 200, 50);
        moneyTextField.setFont(font);
        this.add(moneyTextField);


        sureBtn.setBounds(350, 350, 140, 50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        backBtn.setBounds(520, 350, 140, 50);
        backBtn.addActionListener(this);
        this.add(backBtn);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            // 当期登录的账户
            User loginUser = frame.getLoginUser();
            // 获取输入的金额
            double balance = 0;
            try {
                balance = Double.parseDouble(moneyTextField.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "金额必须为数字");
                return;
            }

            loginUser.setBalance(loginUser.getBalance() + balance);
            //更新数据库
            MysqlUtil.update(loginUser);
            JOptionPane.showMessageDialog(this, "存钱成功");
            frame.switchPanel("menu");

        } else if (e.getSource() == backBtn) {//返回主菜单
            frame.switchPanel("menu");

        }
    }
}
